
<?php

@session_start();

require(dirname(__FILE__) . "/../lib/common.inc");

$status = 0;
$signup_username = "";
$signup_email = "";
$signup_password = "";
$s_json_response = "";

if (isset($_SESSION['username'])) { // 已经登陆
	if ( trim($_SESSION['username']) == '' ) {
		session_unset();
		unset($_SESSION['username']);
	} else {
		$status = -1;
	}
}

if ($status == 0) {
	$b_full_input = true;
	if( !isset($_POST['signup_username'])) { // 未输入登录信息
		$b_full_input = false;
	}
	if( !isset($_POST['signup_email'])) { // 未输入登录信息
		$b_full_input = false;
	}
	if( !isset($_POST['signup_password'])) { // 未输入登录信息
		$b_full_input = false;
	}
	
	if (!$b_full_input) { // 输入信息不完整
		$status = -2;
	} else {              // 输入信息完整有效
		$signup_username = $_POST['signup_username'];
		$signup_email = $_POST['signup_email'];
		$signup_password = $_POST['signup_password'];
		if (
			(trim($signup_username) == "") or
			(trim($signup_email) == "") or
			(trim($signup_password) == "")
		){
			$status = -2;
		}
	}

	if ($status == 0) {
		$b_added = add_new_user($signup_username, $signup_email, $signup_password);
		if (!$b_added) { // 操作数据库失败
			$status = -3;
		} else {         // 操作数据库成功
			$_SESSION['username'] = $signup_username;
			$status = 0;
		}
	}
	
}

$rtn_user = false;
if ($signup_username != '') {
	$rtn_user = get_user_info($signup_username);
}

$s_json_response = "";
$s_json_response .= '{ ';

$s_json_response .= '"' . 'rtn_username' . '": ';
$s_json_response .= '"' . $signup_username  . '", ';

if ($rtn_user) {
	$s_json_response .= '"' . 'rtn_avatar' . '": ';
	$s_json_response .= '"' . $rtn_user['avatar']  . '", ';

	$s_json_response .= '"' . 'rtn_intro' . '": ';
	$s_json_response .= '"' . $rtn_user['introduction']  . '", ';
}

$s_json_response .= '"' . 'status' . '": ';
$s_json_response .= '"' . strval($status)  . '"';

$s_json_response .= ' }';

print($s_json_response);

?>
